package actionbean;

import java.util.ArrayList;
import java.util.Collections;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import controller.CourseCtrl;
import controller.SectionCtrl;
import controller.comparator.SectionComparatorByCourseCodeAndSectionCode;
import entity.Course;
import entity.Section;

@UrlBinding(BaseActionBean.URI_FOR_COURSES
		+ "{$event}/{school}/{courseCode}/{sectionCode}")
public class CoursesActionBean extends BaseBidActionBean {

	// declare parameters
	private String school;

	// init controllers
	private CourseCtrl courseCtrl = new CourseCtrl();
	private SectionCtrl sectionCtrl = new SectionCtrl();

	private ArrayList<Course> courses = new ArrayList<Course>();
	private ArrayList<String> schools = new ArrayList<String>();
	private ArrayList<Section> sections = new ArrayList<Section>();

	public ArrayList<Course> retrievePrerequisitesForCourse(String courseCode) {
		return courseCtrl.retrievePreRequisitesForCourse(courseCode);
	}

	@DefaultHandler
	public Resolution view() {
		loadData();
		return new ForwardResolution(VIEW_FOR_COURSES);
	}

	/*
	 * =========================================================================
	 * Data loaders
	 * =========================================================================
	 */

	@Override
	protected void loadData() {
		super.loadData();
		loadAllSchools();
		loadCoursesForSelectedSchool();
		loadSectionsForSelectedCourseCode();
	}

	private void loadAllSchools() {
		schools = courseCtrl.retrieveAllSchoolsThatHaveCourses();
		schools.add(0, "All");
	}

	private void loadCoursesForSelectedSchool() {
		if (school == null || school.isEmpty()
				|| school.equalsIgnoreCase("All")) {
			school = "All";
			courses = courseCtrl.retrieveAllCourses();
		} else {
			courses = courseCtrl.retrieveCoursesOfferedBySchool(school);
		}
	}

	private void loadSectionsForSelectedCourseCode() {
		if (getCourseCode() != null && !getCourseCode().isEmpty()) {
			sections = sectionCtrl
					.retrieveSectionsForCourseCode(getCourseCode());
			Collections.sort(sections,
					new SectionComparatorByCourseCodeAndSectionCode());
		} else {
			sections.clear();
		}
	}

	/*
	 * =========================================================================
	 * Getters & Setters
	 * =========================================================================
	 */

	public ArrayList<Course> getCourses() {
		return courses;
	}

	public String getSchool() {
		return school;
	}

	public ArrayList<String> getSchools() {
		return schools;
	}

	public ArrayList<Section> getSections() {
		return sections;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	@Override
	protected String redirectUriAfterSuccessfulBidOperation() {
		return URI_FOR_COURSES + "view/" + school + "/" + getCourseCode();
	}

}
